var LightSample = function(l, el) {
	this.l = l;
	this.el = el;
};

LightSample.zero = new LightSample(Vector3.zero, Color.black);

var DirectionLight = function(irradience, direction) {
	this.irradience = irradience;
	this.direction = direction;
	this.shadow = true;
};

DirectionLight.prototype.initialize = function() {
	this.l = this.direction.normalize().negate();
};

DirectionLight.prototype.sample = function(scene, position) {
	var p = position;
	if(this.shadow){
		var ray = new Ray3(position, this.l);
		var result = scene.intersect(ray);
		if(result.geometry){
			return LightSample.zero;
		}
	}
	return new LightSample(this.l, this.irradience);
};

var PointLight = function(intensity, position) {
	this.intensity = intensity;
	this.position = position;
	this.shadow = true;
};

PointLight.prototype.initialize = function() {};

PointLight.prototype.sample = function(scene, position) {
	var delta = this.position.subtract(position);
	var rr = delta.sequare();
	var r = Math.sqrt(rr);
	var l = delta.divide(r);
	if(this.shadow) {
		var ray = new Ray3(position, l);
		var result = scene.intersect(ray);
		if(result.geometry && result.distance <= r) {
			return LightSample.zero;
		}
	}
	return new LightSample(l, this.intensity.multiply(1 / rr));
};

var SpotLight = function(intensity, position, direction, theta, phi, falloff) {
	this.intensity = intensity;
	this.position = position;
	this.direction = direction;
	this.theta = theta;
	this.phi = phi;
	this.falloff = falloff;
	this.shadow = true;
};

SpotLight.prototype.initialize = function() {
	this.s = this.direction.normalize().negate();
	this.cosTheta = Math.cos(this.theta * Math.PI / 180 / 2);
	this.cosPhi = Math.cos(this.phi * Math.PI / 180 / 2);
	this.baseMultiplier = 1 / (this.cosTheta - this.cosPhi);
};

SpotLight.prototype.sample = function(scene, position) {
	var delta = this.position.subtract(position);
	var rr = delta.sequare();
	var r = Math.sqrt(rr);
	var l = delta.divide(r);

	var spot;
	var SdotL = this.s.dot(l);
	if(SdotL >= this.cosTheta) {
		spot = 1;
	} else if(SdotL <= this.cosPhi) {
		spot = 0;
	} else {
		spot = Math.pow((SdotL - this.cosPhi) * this.baseMultiplier, this.falloff);
	}

	if(this.shadow) {
		var ray = new Ray3(position, l);
		var result = scene.intersect(ray);
		if(result.geometry && result.distance <= r) {
			return LightSample.zero;
		}
	}
	return new LightSample(l, this.intensity.multiply(spot / rr));
};